import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:async';
import 'dart:io';
import './apidomain.dart';
import './httpHeaders.dart';
import 'package:shared_preferences/shared_preferences.dart';
class DioUtil{
    static Dio dio = new Dio();

    static  Future request(context,url,{formData})async{
        print("-----111--------");
        try{
            Response response;
            dio.options.headers = httpHeaders;
            // dio.options.contentType = ContentType.parse("application/json;charset=UTF-8");
            showDialog(context);
            if(formData == null){
                response = await dio.post(serviceUrl+url);
            }else{
                response = await dio.post(serviceUrl+url,data:formData);
            }
            if(response.statusCode == 200){
                print("-------------");
                print(response);
                print("-------------");
                Navigator.of(context).pop();
                return response;
            }else{
                Navigator.of(context).pop();
                throw Exception("接口异常R");
            }
        }catch(e){
            Navigator.of(context).pop();
            print("网络出现错误${e}");
        }
    }

    static tokenInter(){
        dio.interceptors.add(InterceptorsWrapper(
            onRequest:(RequestOptions options){
                dio.lock();
                Future<dynamic> future = Future(()async{
                    SharedPreferences prefs =await SharedPreferences.getInstance();
                    return prefs.getString("loginToken");
                });
                return future.then((value) {
                    options.headers["Authorization"] = value;
                    return options;
                }).whenComplete(() => dio.unlock()); // unlock the dio
            },
            onResponse:(Response response) {
                // 在返回响应数据之前做一些预处理
                return response; // continue
            },
            onError: (DioError e) {
                // 当请求失败时做一些预处理
                return e;//continue
            }
        ));
    }
    
    // 加载中的动画
    static showDialog(context){
        return showGeneralDialog(
            context: context,
            barrierLabel: "",
            barrierDismissible: false,
            transitionDuration: Duration(milliseconds: 100), //这个是出现widget的时间
            pageBuilder: (BuildContext context, Animation animation,Animation secondaryAnimation) {
                return Center(
                    child: Container(
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(10.0),
                            boxShadow: [
                                BoxShadow(
                                    color: Colors.grey[200],
                                    blurRadius: 5.0 
                                )
                            ],
                        ),
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                                Image.asset(
                                    'image/loading2.gif',
                                    scale:2.0,
                                ),
                                Padding(
                                    padding: EdgeInsets.only(bottom: 10.0),
                                    child: Text(
                                        "加载中···",
                                        style: TextStyle(
                                            color: Colors.black87,
                                            fontSize: 16,
                                            decoration: TextDecoration.none,
                                        ),
                                    ),
                                )
                            ],
                        )
                    ),
                );
            },
        );
    }
}